<?php

namespace app\common\service\shop\mercadolibre;

/**
 * Goods
 * @package app\common\service
 * @Description 美客多跨境平台接口服务-商品相关接口
 * @author xuli
 * @date 2024-10-21 14:40
 */
class Goods extends BaseApi
{

    /**
     * 添加商品
     * @return void
     */
    public function addItems()
    {
        // 拼接商品信息
        $data = [
            "listing_type_id" => "gold_pro",
            "title" => "Test Item - for LoadTest tests",
            "available_quantity" => 100,
            "category_id" => "CBT11796",
            "buying_mode" => "buy_it_now",
            "currency_id" => "USD",
            "condition" => "new",
            "site_id" => "CBT",
            "price" => 15.1,
            "accepts_mercadopago" => true,
            "non_mercado_pago_payment_methods" => [],
            "warranty" => "1 year",
            "sale_terms" => [
                [
                    "id" => "WARRANTY_TIME",
                    "value_name" => "90 days"
                ],
                [
                    "id" => "WARRANTY_TYPE",
                    "value_id" => "2230279"
                ]
            ],
            "attributes" => [
                [
                    "id" => "PACKAGE_HEIGHT",
                    "value_name" => "10 cm"
                ],
                [
                    "id" => "FOOTWEAR_TYPE",
                    "value_name" => "Zapatilla"
                ],
                [
                    "id" => "PACKAGE_WIDTH",
                    "value_name" => "10 cm"
                ],
                [
                    "id" => "PACKAGE_LENGTH",
                    "value_name" => "10 cm"
                ],
                [
                    "id" => "PACKAGE_WEIGHT",
                    "value_name" => "0.7 lb"
                ],
                [
                    "id" => "BRAND",
                    "name" => "Brand",
                    "value_id" => null,
                    "value_name" => "DOMINIQ",
                ],
                [
                    "id" => "GENDER",
                    "name" => "Gender",
                    "value_id" => "371795",
                    "value_name" => "Babies",
                ],
                [
                    "id" => "ITEM_CONDITION",
                    "name" => "Item condition",
                    "value_id" => "2230284",
                    "value_name" => "New",
                ],
                [
                    "id" => "MODEL",
                    "name" => "Model",
                    "value_id" => null,
                    "value_name" => "A-59081-6",
                ],
                [
                    "id" => "COLOR",
                    "name" => "Color",
                    "tags" => [
                        "allow_variations" => true,
                        "defines_picture" => true,
                        "required" => true
                    ],
                    "hierarchy" => "CHILD_PK",
                    "relevance" => 1,
                    "value_type" => "string",
                    "value_max_length" => 255,
                    "values" => [
                        [
                            "id" => "52010"
                        ]
                    ]
                ],
                [
                    "id" => "SIZE",
                    "name" => "Size",
                    "value_id" => null,
                    "value_name" => "7 M US Women / 5.5 M US Men",
                    "tags" => [
                        "allow_variations" => true,
                        "required" => true
                    ],
                    "hierarchy" => "CHILD_PK",
                    "relevance" => 1,
                    "value_type" => "string",
                    "value_max_length" => 255,
                ],
                [
                    "id" => "GTIN",
                    "name" => "Universal product code",
                    "value_name" => "9102110000",
                    "tags" => [
                        "multivalued" => true,
                        "required" => true,
                        "variation_attribute" => true,
                        "used_hidden" => true,
                        "validate" => true
                    ],
                    "hierarchy" => "PRODUCT_IDENTIFIER",
                    "relevance" => 1,
                    "type" => "product_identifier",
                    "value_type" => "string",
                    "value_max_length" => 255,
                    "tooltip" => "How do I recognize it? It is an 8 to 14 digit number next to the barcode, on the product box or on its label.\n\n![Universal product code](https://http2.mlstatic.com/static/org-img/sd-landings/assets/pi-tooltip.png)",
                ]
            ],
            "pictures" => [
                [
                    "source" => "http://www.mercadolibre.com/jm/img?s=STC&v=O&f=proccesing_image_es.jpg"
                ]
            ]
        ];

        $result = $this->shopApi('add_items', '1', $data);
    }

    /**
     * 获取用户站点列表
     * @return void
     */
    public function getMarketplace()
    {
        // 获取到用户uid
        $user_id = '471826944';

        $result = $this->shopApi('add_items', '1', ['user_id' => $user_id]);

        $return_data = [
            "user_id" => 471826944,
            "site_id" => "CBT",
            "marketplaces" => [
                [
                    "user_id" => 471828584,
                    "site_id" => "MLM",
                    "logistic_type" => "remote"
                ],
                [
                    "user_id" => 471830260,
                    "site_id" => "MLB",
                    "logistic_type" => "remote"
                ],
                [
                    "user_id" => 538932857,
                    "site_id" => "MLC",
                    "logistic_type" => "remote"
                ]
            ]
        ];
    }

    /**
     * 搜索订单
     * @return array[]
     */
    public function getGoodsList()
    {
        $item_ids = [
            'CBT910553725',
            'CBT910547444'
        ];

        $result = $this->shopApi('search_goods', '1', ['item_ids' => implode(',', $item_ids)]);

        $return_data = [
            [
                "code" => 200,
                "body" => [
                    "title" => "Test Item - For Loadtest Tests",
                    "id" => "CBT910547444",
                    "category_id" => "CBT11796",
                    "price" => 15.1
                ]
            ],
            [
                "code" => 200,
                "body" => [
                "category_id" => "CBT11796",
                    "price" => 15.1,
                    "id" => "CBT910553725",
                    "title" => "Test Item - For Loadtest Tests"
                ]
            ]
        ];

        return $return_data;
    }

}
